<template>
  <div class="cart">
    <order-header title="我的购物车">
      <template v-slot:tip>
        <span>温馨提示：产品是否购买成功，以最终下单为准哦，请尽快结算</span>
      </template>
    </order-header>
    <div class="wrapper">
      <div class="container">
        <div class="cart-box">
          <ul class="cart-item-head">
            <li class="col-1">
              <span
                  class="checkbox"
                  v-bind:class="{ checked: allChecked }"
                  @click="toggleAll"
              ></span
              >全选
            </li>
            <li class="col-3">商品名称</li>
            <li class="col-1">单价</li>
            <li class="col-2">数量</li>
            <li class="col-1">小计</li>
            <li class="col-1">操作</li>
          </ul>
          <ul class="cart-item-list">
            <li class="cart-item" v-for="item in cartList" :key="item.id">
              <div class="item-check" @click="handlSilger(item)">
                <span
                    :class="item.selected === 1 ? 'checkbox checked' : 'checkbox'"
                ></span>
              </div>
              <div class="item-name">
                <img :src="item.productImage" alt=""/>
                <span>{{ item.productName }}</span>
              </div>
              <div class="item-price">{{ item.price }}元</div>
              <div class="item-num">
                <div class="num-box">
                  <a href="javascript:;" @click="subGoods(item)">-</a>
                  <span>{{ item.quantity }}</span>
                  <a href="javascript:;" @click="addGoods(item)">+</a>
                </div>
              </div>
              <div class="item-total">{{ item.price * item.quantity }}元</div>
              <div class="item-del" @click="handleDel(item)"></div>
            </li>
          </ul>
        </div>
        <div class="order-wrap clearfix">
          <div class="cart-tip fl">
            <a href="/">继续购物</a>
            共<span>{{ cartList.length }}</span
          >件商品，已选择<span>{{ selectCount }}</span
          >件
          </div>
          <div class="total fr">
            合计：<span>{{ tatol }}</span
          >元
            <a href="javascript:;" class="btn" @click="order">去结算</a>
          </div>
        </div>
      </div>
    </div>
    <service-bar></service-bar>
    <nav-footer></nav-footer>
  </div>
</template>
<script>
import OrderHeader from './../components/OrderHeader';
import ServiceBar from './../components/ServiceBar';
import NavFooter from './../components/NavFooter';
import {
  getCartList,
  deleteCartGoods,
  changeGoodsSelect,
  changeGoodsSelectAll,
  updateCartNumber,
} from '@/api/goods';

export default {
  name: 'index',
  components: {
    OrderHeader,
    ServiceBar,
    NavFooter,
  },
  computed: {
    tatol() {
      let total = 0;
      this.cartList.forEach((item) => {
        console.log(2222);

        if (item.selected === 1) {
          total = total + item.quantity * item.price;
        }
      });
      return total;
      // return this.cartList.reduce(
      //   (total, curItem) => total + curItem.quantity * curItem.price,
      //   0
      // );
    },
    selectCount() {
      let num = 0;
      this.cartList.map((i) => {
        if (i.selected === 1) {
          num++;
        }
      });
      return num;
    },
    allChecked() {
      let isChecked = this.cartList.every((item) => {
        console.log(item);
        return item.selected === 1;
      });
      return isChecked;
    },
  },
  data() {
    return {
      count: 1,
      cartList: [],
    };
  },
  created() {
    this.handleGetCartList();
  },
  methods: {
    // 购物车下单
    order() {
      this.$router.push('/order/confirm');
    },
    // 购物车列表
    async handleGetCartList() {
      const res = await getCartList();
      if (res.data.length >= 0) {
        this.cartList = res.data;
      }

    },
    // 增加
    addGoods(item) {
      this.cartList.map(async (c) => {
        if (c.id === item.id) {
          c.quantity++;
          const res = await updateCartNumber({
            productId: item.productId,
            count: c.quantity,
          });
          if (res) {
            this.handleGetCartList();
          }
        }
      });
    },
    // 减少
    subGoods(item) {
      this.cartList.map(async (c) => {
        if (c.id === item.id) {
          if (c.quantity === 1) {
            return;
          } else {
            c.quantity--;
            const res = await updateCartNumber({
              productId: item.productId,
              count: c.quantity,
            });
            if (res) {
              this.handleGetCartList();
            }
          }
        }
      });
    },
    async toggleAll() {
      let selected = this.allChecked ? 0 : 1;
      if (selected === 1) {
        this.allChecked = true;
        this.cartList.map((i) => {
          i.selected = 1;
        });
      } else {
        this.allChecked = false;
        this.cartList.map((i) => {
          i.selected = 0;
        });
      }
      const res = await changeGoodsSelectAll({selected});
      if (res) {
        console.log(res);
        this.handleGetCartList();
      }
    },
    async handlSilger(item) {
      this.cartList.map((i) => {
        if (item.id === i.id) {
          i.selected ? 0 : 1;
        }
      });
      let isState = item.selected === 1 ? 0 : 1;
      const res = await changeGoodsSelect({
        productId: item.productId,
        selected: isState,
      });
      if (res) {
        this.handleGetCartList();
      }
    },
    async handleDel(item) {
      const res = await deleteCartGoods({productId: item.productId});
      if (res.code === 10000) {
        console.log(res);
        this.handleGetCartList();
      }
    },
  },
};
</script>
<style lang="scss">
.cart {
  .wrapper {
    background-color: #f5f5f5;
    padding-top: 30px;
    padding-bottom: 114px;

    .cart-box {
      background-color: #fff;
      font-size: 14px;
      color: #999999;
      text-align: center;

      .checkbox {
        display: inline-block;
        width: 22px;
        height: 22px;
        border: 1px solid #e5e5e5;
        vertical-align: middle;
        margin-right: 17px;
        cursor: pointer;

        &.checked {
          background: url('/imgs/icon-gou.png') #ff6600 no-repeat center;
          background-size: 16px 12px;
          border: none;
        }
      }

      .cart-item-head {
        display: flex;
        height: 79px;
        line-height: 79px;

        .col-1 {
          flex: 1;
        }

        .col-2 {
          flex: 2;
        }

        .col-3 {
          flex: 3;
        }
      }

      .cart-item-list {
        .cart-item {
          display: flex;
          align-items: center;
          height: 125px;
          border-top: 1px solid #e5e5e5;
          font-size: 16px;

          .item-check {
            flex: 1;
          }

          .item-name {
            flex: 3;
            font-size: 18px;
            color: #333333;
            display: flex;
            align-items: center;

            img {
              width: 80px;
              height: 80px;
              vertical-align: middle;
            }

            span {
              margin-left: 30px;
            }
          }

          .item-price {
            flex: 1;
            color: #333333;
          }

          .item-num {
            flex: 2;

            .num-box {
              display: inline-block;
              width: 160px;
              height: 40px;
              line-height: 40px;
              border: 1px solid #e5e5e5;
              font-size: 14px;

              a {
                display: inline-block;
                width: 50px;
                color: #333333;
              }

              span {
                display: inline-block;
                width: 50px;
                color: #333333;
              }
            }
          }

          .item-total {
            flex: 1;
            color: #ff6600;
          }

          .item-del {
            flex: 1;
            width: 14px;
            height: 12px;
            background: url('/imgs/icon-close.png') no-repeat center;
            background-size: contain;
            cursor: pointer;
          }
        }
      }
    }

    .order-wrap {
      font-size: 14px;
      color: #666666;
      margin-top: 20px;
      height: 50px;
      line-height: 50px;

      .cart-tip {
        margin-left: 29px;

        a {
          color: #666666;
          margin-right: 37px;
        }

        span {
          color: #ff6600;
          margin: 0 5px;
        }
      }

      .total {
        font-size: 14px;
        color: #ff6600;

        span {
          font-size: 24px;
        }

        a {
          width: 202px;
          height: 50px;
          line-height: 50px;
          font-size: 18px;
          margin-left: 37px;
        }
      }
    }
  }
}
</style>
